// pages/cooperation-network/cooperation-network.js
const util = require('../../utils/util.js');
import api from '../../utils/request/api'
import {
	cooperation
} from '../../utils/request/apiurl'


Page({
  data: {
	cooperationList:[],
	id:'',
	value:'',
	tid:'',//收货地址id
	relationName:'',
	linkPhone:'',
	gId:'',//购物车id
  parems:{
    latitude: "",
    longitude: ""
  }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(wx.getStorageSync('userInfo'))
	  if(options.gId){
		  this.setData({
			  // tid:options.tid,
			  // relationName:options.relationName,
			  // linkPhone:options.linkPhone,
			  gId:options.gId,
		  })
	  }else{
		  this.setData({
			  // tid:options.tid,
			  // relationName:options.relationName,
			  // linkPhone:options.linkPhone,
		  })
	  }
    if (wx.getStorageSync('userInfo').latitude != undefined && wx.getStorageSync('userInfo').latitude != null && wx.getStorageSync('userInfo').latitude != '' && wx.getStorageSync('userInfo').longitude != undefined && wx.getStorageSync('userInfo').longitude != null && wx.getStorageSync('userInfo').longitude != ''){
      let paremsList={
        latitude: wx.getStorageSync('userInfo').latitude,
        longitude: wx.getStorageSync('userInfo').longitude,
      }
      this.setData({
        parems: paremsList
      })
    }
	this.cooperationList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
		wx.showNavigationBarLoading() //在标题栏中显示加载
		this.setData({
			cooperationList:[]
		})
		this.cooperationList()
		setTimeout(function() {
			wx.hideNavigationBarLoading() //完成停止加载
			wx.stopPullDownRefresh() //停止下拉刷新
		}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  cooperationList(){
	  api.post(cooperation.list,this.data.parems).then(res => {
		  for(var i=0;i<res.length;i++){
		  	res[i]['checked']=false;
		  }
		this.setData({
			cooperationList:res
		})
		// console.log(this.data.cooperationList)
	  }).catch(err => {
	  	// console.log(err)
	  	wx.showToast({
	  		title: err.message,
	  		icon: 'none'
	  	})
	  }) 
  },
  checkboxChange(e){
	  // console.log(e)
	  this.setData({
		  value:e.detail.value,
		  id:e.detail.value
	  })
	  // var cooperationList=this.data.cooperationList
	  // var Index = e.currentTarget.dataset.index
	  // // console.log(this.data.value.length)
	  // if(cooperationList[Index].checked==false){
			// cooperationList[Index].checked=true;
			// this.setData({
			// 	cooperationList:cooperationList
			// })
	  // }else if(cooperationList[Index].checked==true){
		 //  cooperationList[Index].checked=false
		 //  this.setData({
			// cooperationList:cooperationList
		 //  })
	  // }
  },
  // 返回到上一页
  backToOrderDetail(){
	  // console.log(this.data.id,'444',this.data.tid,'this.data.tidthis.data.tid')
  	if(this.data.id!=''&&this.data.id != null && this.data.id != undefined){
		if(this.data.tid!=''&&this.data.tid != null && this.data.tid != undefined){
			// wx.navigateBack({
			//   delta: 1
			// })
			// console.log(this.data.id,'this.data.id,this.data.id,')
			var pages = getCurrentPages(); // 获取页面栈
			var currPage = pages[pages.length - 1]; // 当前页面
			var prevPage = pages[pages.length - 2]; // 上一个页面
			// console.log(prevPage.data,'prevPage.data.addressInfor')
			prevPage.setData({
				cooperationId:this.data.id,
			})
			wx.navigateBack({
			    delta:1
			})
			// wx.redirectTo({
			// 	url: '../edit-adress/edit-adress?cooperId='+this.data.id+'&tid='+this.data.tid+'&relationName='+this.data.relationName+"&linkPhone="+this.data.linkPhone+'&gId='+this.data.gId
			// })
		}else{
			var pages = getCurrentPages(); // 获取页面栈
			var currPage = pages[pages.length - 1]; // 当前页面
			var prevPage = pages[pages.length - 2]; // 上一个页面
			// console.log(prevPage.data,'prevPage.data.addressInfor')
			prevPage.setData({
				cooperationId:this.data.id,
			})
			wx.navigateBack({
			  delta:1
			})
			// wx.redirectTo({
			// 	url: '../add-receiveingAdress/add-receiveingAdress?cooperId='+this.data.id+'&tid='+this.data.tid+'&relationName='+this.data.relationName+"&linkPhone="+this.data.linkPhone
			// })
		}
  		
  	}	
  },
})